

/*
由于erpnext.SerialBatchPackageSelector 此类 是在  buying.js 中动态调用的
let path = "assets/erpnext/js/utils/serial_no_batch_selector.js";

frappe.require(path, function() {
	new erpnext.SerialBatchPackageSelector(
		me.frm, item, (r) => {
			if (r) {
				let qty = Math.abs(r.total_qty);
				if (doc.is_return) {
					qty = qty * -1;
				}

				let update_values = {
					"serial_and_batch_bundle": r.name,
					"qty": qty
				}

				if (r.warehouse) {
					update_values["warehouse"] = r.warehouse;
				}

				frappe.model.set_value(item.doctype, item.name, update_values);
			}
		}
	);
});

一般方法扩展类方法失效了

【erpnext.SerialBatchPackageSelector=  class  MySerialBatchPackageSelector extends erpnext.SerialBatchPackageSelector {}】
所以采用以下的方法，最终生效了

*/

let path = "assets/erpnext/js/utils/serial_no_batch_selector.js";

frappe.require(path, function() {

erpnext.SerialBatchPackageSelector=  class  MySerialBatchPackageSelector extends erpnext.SerialBatchPackageSelector {

	constructor(frm, item, callback) {  
        super(frm, item, callback);  
        // 在这里添加你的自定义初始化代码  

		// 类选择器 .class    id 选择器  #id
		// this.dialog.$wrapper.find('.modal-dialog').css("max-width", "90%")

		// this.dialog.$wrapper.find("[data-fieldname='__section_3']").css({
			
		// 		"display": "flex",
        //         "align-items": "center"
		    
		// })

		this.dialog.$wrapper.find("[data-fieldname='__column_3']").css(
			{
				"display": "flex",
                "align-items": "center"
		    }
		)

		this.dialog.$wrapper.find("[data-fieldname='btn_addbatch']").css(
			{
				"display": "flex",
                "align-items": "center",
				"margin-top": "18px"
		    }
		)



		// console.log("我是修改后的Class")
    }  
	get_dialog_fields() {

		// console.log("我是修改后的get_dialog_fields方法")
		let fields = [];

		fields.push({
			fieldtype: 'Link',
			fieldname: 'warehouse',
			label: __('Warehouse'),
			options: 'Warehouse',
			default: this.get_warehouse(),
			onchange: () => {
				this.item.warehouse = this.dialog.get_value('warehouse');
				this.get_auto_data()
			},
			get_query: () => {
				return {
					filters: {
						'is_group': 0,
						'company': this.frm.doc.company,
					}
				};
			}
		});

		if (this.frm.doc.doctype === 'Stock Entry'
			&& this.frm.doc.purpose === 'Manufacture') {
			fields.push({
				fieldtype: 'Column Break',
			});

			fields.push({
				fieldtype: 'Link',
				fieldname: 'work_order',
				label: __('For Work Order'),
				options: 'Work Order',
				read_only: 1,
				default: this.frm.doc.work_order,
			});

			fields.push({
				fieldtype: 'Section Break',
			});
		}

		fields.push({
			fieldtype: 'Column Break',
		});

		if (this.item.has_serial_no) {
			fields.push({
				fieldtype: 'Data',
				options: 'Barcode',
				fieldname: 'scan_serial_no',
				label: __('Scan Serial No'),
				get_query: () => {
					return {
						filters: this.get_serial_no_filters()
					};
				},
				onchange: () => this.update_serial_batch_no()
			});
		}

		if (this.item.has_batch_no && !this.item.has_serial_no) {
			fields.push({
				fieldtype: 'Data',
				options: 'Barcode',
				fieldname: 'scan_batch_no',
				label: __('Scan Batch No'),
				onchange: () => this.update_serial_batch_no()
			});
		}

		if (this.item?.type_of_transaction === "Outward") {
			fields = [...this.get_filter_fields(), ...fields];
		} else {
			fields = [...fields, ...this.get_attach_field()];
		}

		fields.push({
			fieldtype: 'Section Break',
		});
       
		
		// 自己添加的

		fields.push({
			fieldtype: 'Data',
			fieldname: 'input_batch_no',
			label: __('Batch No/Serial No'),
			// onchange: () => this.update_serial_batch_no()
		});
		fields.push({
			fieldtype: 'Column Break',
		});

		fields.push(
			{
			 fieldtype: 'Button',
			 fieldname: 'btn_addbatch',
			 label: __('Add Batch No/Serial No'),
			 click: () => this.insert_batch_nos()
			 });

		fields.push({
			fieldtype: 'Section Break',
		});

	

		// 自己添加的结束
		fields.push({
			fieldname: 'entries',
			fieldtype: 'Table',
			allow_bulk_edit: true,
			data: [],
			fields: this.get_dialog_table_fields(),
		});

		return fields;
	};

    // 自己添加
	insert_batch_nos(){

		const {  input_batch_no } = this.dialog.get_values();
	
		if (this.item.has_batch_no) {
				if (input_batch_no) {
					let existing_row = this.dialog.fields_dict.entries.df.data.filter(d => {
						if (d.batch_no === input_batch_no) {
							return d
						}
					});
		
					if (existing_row?.length) {
						existing_row[0].qty += 1;
					} else {
						this.dialog.fields_dict.entries.df.data.push({
							batch_no: input_batch_no,
							qty: 1
						});
					}
		
					this.dialog.fields_dict.input_batch_no.set_value('');
				}
			}else{

				if (input_batch_no) {
					let existing_row = this.dialog.fields_dict.entries.df.data.filter(d => {
						if (d.serial_no === input_batch_no) {
							return d
						}
					});
		
					if (existing_row?.length) {
						existing_row[0].qty += 1;
					} else {
						this.dialog.fields_dict.entries.df.data.push({
							serial_no: input_batch_no
						});
					}
		
					this.dialog.fields_dict.input_batch_no.set_value('');
				}

			}

		this.dialog.fields_dict.entries.grid.refresh();


		// 文本框获得焦点
		this.dialog.$wrapper.find("input[data-fieldname='input_batch_no']").focus();

		// this.dialog.$wrapper.find("input[data-fieldname='scan_batch_no']").focus();



	}
	// 自己添加结束

}

})

